﻿using AutoMapper;
using CodeSaude.Application.Interfaces;
using CodeSaude.Application.ViewModels;
using CodeSaude.Domain.Entities;
using CodeSaude.Domain.Interfaces.Service;
using System;
using System.Collections.Generic;

namespace CodeSaude.Application.Services
{
    public class DietaApplicationService : BaseApplicationService, IDietaApplicationService
    {
        private readonly IDietaService _dietaService;

        public DietaApplicationService(IDietaService dietaService)
        {
            _dietaService = dietaService;
        }

        public void Create(DietaViewModel model)
        {
            try
            {
                Dieta dieta = Mapper.Map<DietaViewModel, Dieta>(model);

                BeginTransaction();
                _dietaService.Add(dieta);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Dispose()
        {
            _dietaService.Dispose();
        }

        public IEnumerable<DietaViewModel> GetAll()
        {
            try
            {
                return Mapper.Map<IEnumerable<DietaViewModel>>(_dietaService.GetAll());
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DietaViewModel GetById(int id)
        {
            try
            {
                return Mapper.Map<DietaViewModel>(_dietaService.GetById(id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void Delete(DietaViewModel model)
        {
            try
            {
                Dieta dieta = _dietaService.GetById(model.DietaId);
                BeginTransaction();
                _dietaService.Remove(dieta);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Update(DietaViewModel model)
        {
            try
            {
                Dieta dieta = Mapper.Map<Dieta>(model);
                BeginTransaction();
                _dietaService.Update(dieta);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
